package com.youngball.netty;

import CommandEntity.HardWare;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liaopingzhu on 2016/12/15.
 */
public class LightServerInitHandler extends ChannelInboundHandlerAdapter {

    public final static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    public static Map channelMap = new HashMap();

    public static HardWare.LightRequest localstate = null;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //硬件信息
//        RedisDao redisDao = new RedisDao("127.0.0.1",6379);
        String remoteAddess= ctx.channel().remoteAddress().toString();
        CommandEntity.HardWare.LightRequest request = (CommandEntity.HardWare.LightRequest)msg;
        boolean flag = requestOrResponse(request);

        if(flag){
            //若收到request命令
            if(request.getCmdValue() == 3){
                //若是心跳
                System.out.println(remoteAddess+"请求如下:"+"客户端Id"+request.getCid()
                        +" 版本号:"+request.getVersion()+" 指令内容cmd:"+request.getCmdValue()
                        +"指令类型:"+request.getCmdValue());
                //保存树莓派客户端chanel到集合中去
                channelMap.put(request.getCid(),ctx.channel());
            }else if(request.getCmdValue() == 0 || request.getCmdValue() == 1){
                //若是开关灯命令
                System.out.println("==========接收开关灯命令,cid为========: "+request.getCid());
                //保存web客户端chanel到集合中去
                channelMap.put(request.getCid(),ctx.channel());
                //对出web客户端对应的树莓派
                Channel channel = (Channel) channelMap.get(request.getCid().split("club")[1]);
                //向树莓派发送命令
                channel.writeAndFlush(request);
                System.out.println("向树莓派发送内容:"+request);
            }else if(request.getCmdValue() == 2){
                //若是查询命令
                System.out.println("=====接收查询灯控的命令,cid为=====:"+request.getCid());
                channelMap.put(request.getCid(),ctx.channel());
                Channel channel = (Channel) channelMap.get(request.getCid().split("club")[1]);
                channel.writeAndFlush(request);
                System.out.println("向树莓派发送内容:"+request);
            }
        }else {
            //若收到response命令
            CommandEntity.HardWare.LightRequest response = (CommandEntity.HardWare.LightRequest)msg;
            String webCid = "club" + response.getCid();
            Channel channel = (Channel)channelMap.get(webCid);
            System.out.println("返回给web客户端的内容为:"+response.getCid()+response.getResultMessage()+response.getResultCode());
            localstate = response;
            if(channel != null){
                channel.writeAndFlush(response);
            }
        }


    }

    private boolean requestOrResponse(CommandEntity.HardWare.LightRequest request) {

        if(request.getResultCode() == 0 && request.getResultData().equals("")
                && request.getResultMessage().equals("")){
            //request
            return true;
        }else{
            //response
            return false;
        }

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
